
package com.jl.cloud.commom.base;


import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.jl.cloud.commom.exception.BaseException;
import com.jl.cloud.commom.exception.SystemExceptionEnum;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Controller基类
 * @author liang.jun
 */
@ControllerAdvice
@Slf4j
public  class GlobalExceptionHandle {

    /**
     * 统一异常处理
     *
     * @param ex
     * @param request
     * @return
     */
    @ExceptionHandler(BlockException.class)
    @ResponseBody
    public BaseResponse<?> handleException(FlowException ex, HttpServletRequest request) {
        log.error("接口（" + request.getRequestURI() + "）发生异常：", ex);
        BaseResponse<?> response = new BaseResponse<>();
        response.setResponseCode(SystemExceptionEnum.EROOR_02010102.getCode());
        response.setMessage(SystemExceptionEnum.EROOR_02010102.getMessage());
        return response;
    }


    /**
     * 统一异常处理
     * 
     * @param ex
     * @param request
     * @return
     */
    @ExceptionHandler(BaseException.class)
    @ResponseBody
    public BaseResponse<?> handleBaseException(BaseException ex, HttpServletRequest request) {
        log.error("接口（" + request.getRequestURI() + "）发生异常：", ex);
        BaseResponse<?> response = new BaseResponse<>();
        response.setResponseCode(ex.getErrorCode());
        response.setMessage(ex.getErrorMsg());
        return response;
    }

    /**
     * 统一异常处理
     *
     * @param ex
     * @param request
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public BaseResponse<?> handleException(Exception ex, HttpServletRequest request) {
        log.error("接口（" + request.getRequestURI() + "）发生异常：", ex);
        BaseResponse<?> response = new BaseResponse<>();
        response.setResponseCode(SystemExceptionEnum.EROOR_02010101.getCode());
        response.setMessage(SystemExceptionEnum.EROOR_02010101.getMessage());
        return response;
    }


}
